﻿// ------------------------------------------------------------------------------
// <copyright file="DateTimeConverter.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.
//
//    This program is free software; you can redistribute it and/or
//    modify it under the terms of the GNU General Public License
//    as published by the Free Software Foundation version 2
//    of the License.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    http://www.gnu.org/licenses/gpl-2.0.html
//
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLib.Converters
{
    using System;
    using System.Globalization;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Data;

    /// <summary>
    /// Date time value converter
    /// </summary>
    public class DateTimeConverter : IValueConverter
    {
        /// <summary>
        /// Convert DateTime during binding to formatted string value
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="language">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null || !(value is DateTime))
            {
                return null;
            }

            DateTime date = (DateTime)value;

            if (date == DateTime.MinValue)
            {
                return null;
            }

            if (parameter == null)
            {
                return date.ToString();
            }
            else
            {
                return date.ToString(parameter.ToString());
            }
        }

        /// <summary>
        /// Convert string value to DateTime data when propagates the value from the binding target to the binding source
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="language">The culture to use in the converter.</param>
        /// <returns>A converted value. If the method returns null, the valid null value is used.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value == null || targetType != typeof(DateTime))
            {
                return DependencyProperty.UnsetValue;
            }

            DateTime date = DateTime.Now;
            DateTime.TryParseExact(value.ToString(), parameter.ToString(), null, DateTimeStyles.None, out date);
            return date;
        }
    }
}